import axios, { AxiosError, AxiosResponse } from 'axios'
// @ts-ignore
import { vue } from '@/main'

import { Loading, Message } from 'element-ui'

let requestQueue: string[] = []

let loadinginstace: any

// 请求拦截
axios.defaults.baseURL = '/'
axios.interceptors.request.use((config) => {
  config.headers.token = window.sessionStorage.getItem('token') || ''
  // let currentReq = `${config.url}&&${config.method}`
  // if (requestQueue.includes(currentReq)) {
  //   return Promise.reject({
  //     data: null,
  //     response: {
  //       status: 600,
  //       statusText: 'sss'
  //     }
  //   })
  // }
  // requestQueue.push(currentReq)
  loadinginstace = Loading.service({ fullscreen: true })
  // console.log(loadinginstace)
  return config
}, (error) => {
  return Promise.reject(error)
})
// 响应拦截
axios.interceptors.response.use((res: AxiosResponse): AxiosResponse | Promise<AxiosResponse> => {
  // requestQueue = requestQueue.filter(task => `${res.config.url}&&${res.config.method}` !== task)
  loadinginstace.close()
  let data = res.data as AjaxResponse
  switch (data.responseCode) {

    case 500:
    // vue.$router.push('/login')
    // vue.tips(data)
      break
    case 4019:
      vue.$router.push('/login')
      vue.$utils.tips(data)
      break
    case undefined:
  }
  // @ts-ignore
  return data
}, (err) => {
  let e: AjaxResponse = {
    data: null,
    responseCode: err.response.status,
    responseDesc: err.response.statusText
  }
  return Promise.resolve(e)
})
export default axios
